package smcms.service.product.service.impl;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import smcms.commons.api.entity.result.CommonsResult;
import smcms.service.product.entity.*;
import smcms.service.product.mapper.ProductMapper;
import smcms.service.product.service.ProductService;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CommonsResult commonsResult;

    @Override
    public CommonsResult insertProduct(ProductReq productReq) {
        productMapper.insertProduct(productReq);
        return commonsResult.success("插入成功");
    }

    @Override
    public CommonsResult updateProduct(ProductReq productReq) {
        if (ObjectUtils.isEmpty(productReq.getProductId())) {
            return commonsResult.failed("要修改商品的ID不能为空！");
        }
        productMapper.updateProduct(productReq);
        return commonsResult.success("更新成功！");
    }

    @Override
    public CommonsResult deleteProduct(ProductReq productReq) {
        if (ObjectUtils.isEmpty(productReq.getProductId())) {
            return commonsResult.failed("无法获取ID，删除失败！");
        }
        productMapper.deleteProduct(productReq);
        return commonsResult.success("删除成功！");
    }

    @Override
    public List<ProductResp> queryProductList(ProductReq productReq) {
        if (ObjectUtils.isEmpty(productReq)) {
            throw new IllegalArgumentException("用户名不能为空！");
        }
        List<ProductResp> productResps = productMapper.queryProductByOPs(productReq);
        return productResps;
    }

    @Override
    public List<ProductTemp> queryProductExport(ProductReq productReq) {
        if (ObjectUtils.isEmpty(productReq.getCloudId())) {
            throw new IllegalArgumentException("cloudId不能为空！");
        }
        List<ProductTemp> productTemps = productMapper.queryProductExport(productReq);
        return productTemps;
    }

    @Override
    public ProductResp queryProduct(ProductReq productReq) {
        ProductResp productResp = productMapper.queryProductOne(productReq);
        return productResp;
    }

    @Override
    public CommonsResult productInventoryPlus(InventoryPlus inventoryPlus) {
        if (ObjectUtils.isEmpty(inventoryPlus.getPlus())) {
            inventoryPlus.setPlus(0);
        }
        if (ObjectUtils.isEmpty(inventoryPlus.getProductId())) {
            return commonsResult.failed("商品ID不能为空！");
        }
        Integer inventory = productMapper.queryInventory(inventoryPlus);
        if (inventoryPlus.getType().equals("销售订单")) {
            if (inventory >= -1 * inventoryPlus.getPlus()) {
                productMapper.updateProductSale(inventoryPlus);
            }else {
                return commonsResult.failed("库存不足！请检查订单数量！");
            }

        }

        productMapper.updateProductInventory(inventoryPlus);
        return commonsResult.success();
    }

    @Override
    public List<CategoryResp> queryProductType(ProductReq req) {
        List<CategoryResp> categoryRespList = productMapper.queryProductType(req);
        return categoryRespList;
    }
}
